/**
 *
 * MobileLib   
 * BookInfo.java
 * Jan 5, 2012
 * @author qidiven
 *
 */
package com.ustc.mobilelib.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ustc.diven.model.R;
import com.ustc.mobilelib.control.RequestHelper;
import com.ustc.mobilelib.model.DataCenter;

public class BookInfoActivity extends Activity{
	
	private static final String TAG = BookInfoActivity.class.toString();
	private ImageView bookImg = null; 
	private TextView bookName = null; 
	private TextView bookInfo = null;
	private TextView bookIntro = null;
	private Button libPosBtn = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_info);
		init();
	}
	
	private void init()
	{
		this.getWindow().setBackgroundDrawableResource(R.color.loading_bg);
		bookImg = (ImageView) findViewById(R.id.detail_info_book_img);
		bookName = (TextView) findViewById(R.id.detail_info_bookname);
		bookIntro = (TextView) findViewById(R.id.detail_info_description);
		bookInfo = (TextView) findViewById(R.id.detail_info_bookinfo);
		
		libPosBtn = (Button) findViewById(R.id.get_lib_info);
		
		if(DataCenter.getInstance().getCurrUsedBufferViaId(DataCenter.getInstance().getCurrUsedBufferID()).get(DataCenter.getInstance().getSelectBookItem()).getLibPosInfo() == null)
		{
			libPosBtn.setVisibility(View.INVISIBLE);
		}
		
		libPosBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				if(DataCenter.getInstance().getCurrUsedBufferViaId(DataCenter.getInstance().getCurrUsedBufferID()).get(DataCenter.getInstance().getSelectBookItem()).getLibPosInfo() != null)
				{
					try {
						RequestHelper.getBookPos(DataCenter.getInstance().getCurrUsedBufferViaId(DataCenter.getInstance().getCurrUsedBufferID()).get(DataCenter.getInstance().getSelectBookItem()).getLibPosInfo());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Intent intent = new Intent (BookInfoActivity.this, BookPosActivity.class);
					startActivity(intent);
				}				
			}
		});
		
		
		byte[] imgStream = DataCenter.getInstance().getCurrUsedImgBufferViaId(DataCenter.getInstance().getCurrUsedBufferID()).get(DataCenter.getInstance().getSelectBookItem());
		Bitmap bm = BitmapFactory.decodeByteArray(imgStream, 0, imgStream.length);
		bm = Bitmap.createScaledBitmap(bm, getFixedWidth(), getFixedHeight(), false);
		bookImg.setImageBitmap(bm);
		
		bookName.setText(DataCenter.getInstance().getCurrUsedBufferViaId(DataCenter.getInstance().getCurrUsedBufferID()).get(DataCenter.getInstance().getSelectBookItem()).getBookName());
		bookInfo.setText(DataCenter.getInstance().getCurrUsedBufferViaId(DataCenter.getInstance().getCurrUsedBufferID()).get(DataCenter.getInstance().getSelectBookItem()).getAutherName());
		bookIntro.setText(DataCenter.getInstance().getCurrUsedBufferViaId(DataCenter.getInstance().getCurrUsedBufferID()).get(DataCenter.getInstance().getSelectBookItem()).getBookIntroduction());
	}
	
	
	private int getFixedWidth()
	{
		DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        Log.d(TAG, "size" + dm.heightPixels + " " + dm.widthPixels + " " + dm.xdpi + " " + dm.ydpi);
        if (dm.heightPixels == 800)
        	return 80;
        else if(dm.heightPixels == 480)
        	return 50;
        else 
        	return 35;
        	
	}
	
	private int getFixedHeight()
	{
		DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        if (dm.heightPixels == 800)
        	return 100;
        else if(dm.heightPixels == 480)
        	return 70;
        else 
        	return 50;
        	
	}

}
